
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S7. Root Mean Squared Errors (US, UK and Germany), Presidential vs. Legislative Elections, Vote = Polls

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Presidential"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************
keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_PRES_RMSE_EQ1.dta, replace

***********************************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.K. LEGISLATIVE ELECTIONS - VOTE = POLLS + PARTY **********************
***********************************************************************************************************

use MI_COVAR_ALL.dta, clear

keep if election=="Legislative"

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=.
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

***********************************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *********************************************************************
***********************************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_LEG_RMSE_EQ1.dta, replace

*******************************************************************************
** PRESIDENTIAL vs. LEGISLATIVE ELECTIONS *************************************
*******************************************************************************

use MI_PRES_RMSE_EQ1.dta, clear 
rename rmse pres_eqn1_rmse
rename rmse_min95 pres_eqn1_rmse_min95
rename rmse_max95 pres_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_PRES_RMSE_EQ1.dta, replace

use MI_LEG_RMSE_EQ1.dta, clear
rename rmse leg_eqn1_rmse
rename rmse_min95 leg_eqn1_rmse_min95
rename rmse_max95 leg_eqn1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_LEG_RMSE_EQ1.dta, replace

clear
use MI_PRES_RMSE_EQ1.dta, clear 

merge daysbeforeED using MI_LEG_RMSE_EQ1.dta
drop _merge 
sort daysbeforeED

* 
twoway /*
*/  || line pres_eqn1_rmse daysbeforeED,                 /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)    /* 
*/  || line leg_eqn1_rmse daysbeforeED,                  /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)     /* 
*/  || line pres_eqn1_rmse_min95 daysbeforeED,           /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line pres_eqn1_rmse_max95 daysbeforeED,           /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_eqn1_rmse_min95 daysbeforeED,            /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line leg_eqn1_rmse_max95 daysbeforeED,            /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  , scheme(s2mono) graphregion(color(white))           /*
*/  xtitle("Days Until Election")                        /*
*/  title("")                                            /*
*/  ylabel(0(1)8, gmax angle(horizontal))                /*
*/  ytick(0(1)8)                                         /*
*/  ytitle(Root Mean Squared Error)                      /*
*/  xlabel(0(50)200)                                     /*
*/  xscale(reverse) xscale(titlegap(2))                  /*
*/  legend(order(1 "Presidential" 2 "Legislative")       /*
*/  rows(1)                                              /*
*/  size(medsmall))

graph export FigS7.tif
